#!/usr/bin/env python3

import json
import os
import sys
import random

build_dirs = [ "./../build/bin/" , "./../build/py/" ]
targets = [ "tests-128", "tests-256", "tests-dbg-128", "tests-dbg-256", "examples-256", "examples-128", "xrnlib-128-cli", "xrnlib-cli", "xrnlib-256-cli", "_xrnlib256.so", "_xrnlib128.so" ]

target_path = ""
target_name = ""
for build_dir in build_dirs :
    for target in targets :
        if ( os.path.exists(build_dir+target) ):
            target_path = build_dir+target
            target_name = target
            break


if ( target_name == "xrnlib-cli" ) :
    if ( len(sys.argv) == 2 ) :
       cmd="cd ./backend/parser_tests/; bash ./run_parser_tests.sh " + sys.argv[1]
       os.system(cmd);
    else :
       cmd="cd ./backend/parser_tests/; bash ./run_parser_tests.sh "
       os.system(cmd);

if (( target_name == "tests-128" ) or ( target_name == "tests-256" )):
    clean_cache_str = " rm -rf ../cache/* ; "
    cmd = clean_cache_str + " cd ../cache ; cp -r ../archives/resources/code_examples/* . ; " + target_path
    os.system(cmd);

if (( target_name == "tests-dbg-128" ) or ( target_name == "tests-dbg-256" )):
    clean_cache_str = " rm -rf ../cache/* ; "
    cmd = clean_cache_str + " cd ../cache ; cp -r ../archives/resources/code_examples/* . ; valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes " + target_path + " 2>../build/reports/valgrind_stderr.rep  >../build/reports/valgrind_stdout.rep "
    print(cmd)
    os.system(cmd);

if ( target_name == "examples-256" ) or ( target_name == "examples-128" ):
    cache_dir = "../cache" 
    cache_example_dir = cache_dir + "/examples"
    cache_example_init_structures_dir = cache_example_dir + "/datastructures"
    cache_example_init_exec_dir = cache_example_dir + "/exec"

    if ( False == os.path.exists( cache_example_init_structures_dir ) ):
        if ( False == os.path.exists( cache_example_dir )):
            if ( False == os.path.exists( cache_dir )):
                cmd = "mkdir " + cache_dir 
                os.system(cmd);
            cmd = "mkdir " + cache_example_dir 
            os.system(cmd);
        cmd = "mkdir " + cache_example_init_structures_dir 
        os.system(cmd);

        # generate_xrn_matrix_one_third()
        cmd = " cd " + cache_example_init_structures_dir + " ; ../../" + target_path + " " + str(0)
        os.system(cmd);
        # generate_keys_with_wrapper_functions()
        cmd = " cd " + cache_example_init_structures_dir + " ; ../../" + target_path + " " + str(2)
        os.system(cmd);

    if ( False == os.path.exists( cache_example_init_exec_dir )):
        cmd = "mkdir " + cache_example_init_exec_dir  
        os.system(cmd);

    clean_cache_str = " rm -rf " + cache_example_init_exec_dir + "/* "
    os.system(clean_cache_str );

    copy_data = " cp " + cache_example_init_structures_dir + "/xrn_matrix_compressed.xmc " + cache_example_init_exec_dir + " ; "
    copy_data = copy_data + " cp " + cache_example_init_structures_dir + "/xrn_matrix.xm " + cache_example_init_exec_dir  + " ; "
    copy_data = copy_data + " cp " + cache_example_init_structures_dir + "/xbinomial_key.xf " + cache_example_init_exec_dir  + " ; "
    copy_data = copy_data + " cp " + cache_example_init_structures_dir + "/xmonomial_key.xf " + cache_example_init_exec_dir  + " ; "
    copy_data = copy_data + " cp " + cache_example_init_structures_dir + "/xmonovalent_key.xf " + cache_example_init_exec_dir  + " ; "
    copy_data = copy_data + " cp " + cache_example_init_structures_dir + "/xpolyvalent_key.xf " + cache_example_init_exec_dir  + " ; "
    copy_data = copy_data + " cp " + cache_example_init_structures_dir + "/xpolyvalent_proof.xf " + cache_example_init_exec_dir  + " ; "
    copy_data = copy_data + " cp " + cache_example_init_structures_dir + "/xstart_point.xf " + cache_example_init_exec_dir  + " ; "
    copy_data = copy_data + " cp ../archives/resources/code_examples/lorem_ipsum.py " + cache_example_init_exec_dir  + " ; "
    copy_data = copy_data + " cp ../archives/resources/code_examples/lorem_ipsum.txt " + cache_example_init_exec_dir  + " ; "

    if ( len(sys.argv) == 2 ) :
        if ( sys.argv[1] == "all" ):
            for i in range ( 35 ) :
                print("running "+str(i)+"/35")
                os.system(clean_cache_str);
                cmd = copy_data + " cd " + cache_example_init_exec_dir + " ; ../../" + target_path + " " + str(i)
                os.system(cmd)
        else :
            os.system(clean_cache_str);
            cmd = copy_data + " cd " + cache_example_init_exec_dir + " ; ../../" + target_path + " " + sys.argv[1]
            os.system(cmd)
    else :
        os.system(clean_cache_str)
        cmd = target_path 
        os.system(cmd)

if (( target_name == "_xrnlib256.so" ) or ( target_name == "_xrnlib128.so" )):

    ordered_script_list = [ "generate_xrn_matrix_one_third.py", 
                            "generate_xrn_matrix_all_rnd.py", 
                            "generate_keys_with_wrapper_functions.py",
                            "check_xrn_matrix.py", 
                            "split_join_encryption_wrapper.py", 
                            "split_join_encryption_bytes_wrapper.py", 
                            "encrypt_symmetric_wrapper.py", 
                            "encrypt_symmetric_bytes_wrapper.py",
                            "encrypt_asymmetric_wrapper.py",
                            "encrypt_asymmetric_bytes_wrapper.py", 
                            "hash_bytes_wrapper.py", 
                            "hash_file_wrapper.py", 
                            "symmetric_signature_file_wrapper.py",
                            "symmetric_signature_bytes_wrapper.py",
                            "asymmetric_signature_file_wrapper.py", 
                            "asymmetric_signature_bytes_wrapper.py",
                            "pseudo_random_number_generator_wrapper.py", 
                            "ring_signature_file_wrapper.py", 
                            "ring_signature_bytes_wrapper.py",
                            "monomial_key_exchange.py",
                            "certificate_symmetric.py", 
                            "certificate_asymmetric.py",  
                            "zero_knowledge_proof_binomial_key.py", 
                            "zero_knowledge_proof_signature.py",
                            "zero_knowledge_proof_signature_bytes.py", 
                            "zero_knowledge_proof_certificate.py", 
                            "permute_xrn_matrix_compressed_rnd.py",
                            "permute_xrn_matrix_compressed_deterministic.py", 
                            "permute_xrn_matrix_compressed_key.py", 
                            "encoding_decoding_file_wrapper.py",
                            "encoding_decoding_bytes_wrapper.py",
                            "polyvalent_key_exchange.py",
                            "checksum_dump_file_wrapper.py",
                            "checksum_dump_bytes_wrapper.py",
                            "create_blocks_from_file.py",
                            "create_blocks_from_bytes.py",
                            "steganography_block_file.py",
                            "steganography_block_bytes.py",
                            "steganography_raw_file.py",
                            "steganography_raw_bytes.py"
                         ]

    cache_dir = "../cache" 
    cache_example_dir = cache_dir + "/examples"
    cache_example_init_structures_dir = cache_example_dir + "/datastructures"
    cache_example_init_exec_dir = cache_example_dir + "/exec"

    if ( False == os.path.exists( cache_example_init_structures_dir ) ):
        if ( False == os.path.exists( cache_example_dir )):
            if ( False == os.path.exists( cache_dir )):
                cmd = "mkdir " + cache_dir 
                os.system(cmd);
            cmd = "mkdir " + cache_example_dir 
            os.system(cmd);
        cmd = "mkdir " + cache_example_init_structures_dir 
        os.system(cmd);

        #cmd = " cd " + cache_example_init_structures_dir + " ; ../../" + target_path + " " + str(0)
        if target_name == "_xrnlib128.so" :
            cmd = " cp ./../build/py/_xrnlib128.so " + cache_example_init_structures_dir + " ; "
            cmd = cmd + " cp ./../build/py/xrnlib128.py " + cache_example_init_structures_dir + " ; " 
        else :
            cmd = " cp ./../build/py/_xrnlib256.so " + cache_example_init_structures_dir + " ; "
            cmd = cmd + " cp ./../build/py/xrnlib256.py " + cache_example_init_structures_dir + " ; " 

        cmd = cmd + " cp ../code/xrnlib/app/examples/py/" + ordered_script_list[0] + " " + cache_example_init_structures_dir + " ; "
        cmd = cmd + " cp ../code/xrnlib/app/examples/py/" + ordered_script_list[2] + " " + cache_example_init_structures_dir + " ; "
        print("cmd: " + cmd); os.system(cmd);
        # generate_xrn_matrix_one_third()
        if target_name == "_xrnlib128.so" :
            cmd = "sed -i 's/xrnlib256/xrnlib128/' " + cache_example_init_structures_dir + "/" + ordered_script_list[0] 
            print(cmd)
            os.system(cmd);
            cmd = "sed -i 's/xrnlib256/xrnlib128/' " + cache_example_init_structures_dir + "/" + ordered_script_list[2] 
            print(cmd)
            os.system(cmd);
        cmd = "cd " + cache_example_init_structures_dir + " ; python3 " + ordered_script_list[0] + " ;"
        print("cmd: " + cmd); os.system(cmd);
        # generate_keys_with_wrapper_functions()
        cmd = "cd " + cache_example_init_structures_dir + " ; python3 " + ordered_script_list[2] + " ;"
        print("cmd: " + cmd); os.system(cmd);


    if ( False == os.path.exists( cache_example_init_exec_dir )):
        cmd = "mkdir " + cache_example_init_exec_dir  
        os.system(cmd);

    clean_cache_str = " rm -rf " + cache_example_init_exec_dir + "/* "
    os.system(clean_cache_str );

    copy_data = " cp " + cache_example_init_structures_dir + "/xrn_matrix_compressed.xmc " + cache_example_init_exec_dir + " ; "

    if target_name == "_xrnlib256.so" :
        copy_data = copy_data + " cp " + cache_example_init_structures_dir + "/_xrnlib256.so " + cache_example_init_exec_dir  + " ; "
        copy_data = copy_data + " cp " + cache_example_init_structures_dir + "/xrnlib256.py " + cache_example_init_exec_dir  + " ; "
    else :
        copy_data = copy_data + " cp " + cache_example_init_structures_dir + "/_xrnlib128.so " + cache_example_init_exec_dir  + " ; "
        copy_data = copy_data + " cp " + cache_example_init_structures_dir + "/xrnlib128.py " + cache_example_init_exec_dir  + " ; "

    copy_data = copy_data + " cp " + cache_example_init_structures_dir + "/xrn_matrix.xm " + cache_example_init_exec_dir  + " ; "
    copy_data = copy_data + " cp " + cache_example_init_structures_dir + "/xbinomial_key.xf " + cache_example_init_exec_dir  + " ; "
    copy_data = copy_data + " cp " + cache_example_init_structures_dir + "/xmonomial_key.xf " + cache_example_init_exec_dir  + " ; "
    copy_data = copy_data + " cp " + cache_example_init_structures_dir + "/xmonovalent_key.xf " + cache_example_init_exec_dir  + " ; "
    copy_data = copy_data + " cp " + cache_example_init_structures_dir + "/xpolyvalent_key.xf " + cache_example_init_exec_dir  + " ; "
    copy_data = copy_data + " cp " + cache_example_init_structures_dir + "/xpolyvalent_proof.xf " + cache_example_init_exec_dir  + " ; "
    copy_data = copy_data + " cp " + cache_example_init_structures_dir + "/xstart_point.xf " + cache_example_init_exec_dir  + " ; "
    copy_data = copy_data + " cp ../archives/resources/code_examples/lorem_ipsum.py " + cache_example_init_exec_dir  + " ; "
    copy_data = copy_data + " cp ../archives/resources/code_examples/lorem_ipsum.txt " + cache_example_init_exec_dir  + " ; "
    clean_cache_str = " rm -rf " + cache_example_init_exec_dir + "/* "

    runall = 0
    if ( len(sys.argv) == 2 ) :
        if ( sys.argv[1] == "all" ):
            runall = 1
            argvint = 0
        else :
            argvint = int(sys.argv[1])
    if (( len(sys.argv) != 2 ) or ( argvint < 0 ) or ( argvint >= 40 )):
        print( "implemented Python examples:" )
        i = 0
        for script in ordered_script_list :
            print( str(i) + ")  " + ordered_script_list[i] ) ; i = i + 1
        exit(0)
    if ( runall == 0 ) :
        pyscript = ordered_script_list[ argvint ]
        print("\n\nrunning " + pyscript + "\n" )
        os.system(clean_cache_str);
        os.system(copy_data);

        cmd = "cp ../code/xrnlib/app/examples/py/" + pyscript + " " + cache_example_init_exec_dir + " ; "
        os.system(cmd);
        if target_name == "_xrnlib128.so" :
            cmd = "sed -i 's/xrnlib256/xrnlib128/' " + cache_example_init_exec_dir + "/" + pyscript 
            os.system(cmd);
            cmd = "sed -i 's/xrnlib256/xrnlib128/' " + cache_example_init_exec_dir + "/lorem_ipsum.py" 
            os.system(cmd);
        cmd = "cd " + cache_example_init_exec_dir + " ; python3 " + pyscript + " ;"
        os.system(cmd);

    else :
        for i in range (len(ordered_script_list)):
            print("running "+str(i)+"/"+ str(len(ordered_script_list)))
            os.system(clean_cache_str);
            os.system(copy_data);
            pyscript = ordered_script_list[ i ]
            cmd = "cp ../code/xrnlib/app/examples/py/" + pyscript + " " + cache_example_init_exec_dir + " ; "
            os.system(cmd);
            if target_name == "_xrnlib128.so" :
                cmd = "sed -i 's/xrnlib256/xrnlib128/' " + cache_example_init_exec_dir + "/" + pyscript 
                os.system(cmd);
                cmd = "sed -i 's/xrnlib256/xrnlib128/' " + cache_example_init_exec_dir + "/lorem_ipsum.py" 
                os.system(cmd);
            cmd = "cd " + cache_example_init_exec_dir + " ; python3 " + pyscript + " ;"
            os.system(cmd);

    

